# Contributor: Edward E. <develinthedetail@gmail.com>

_realname=libheif
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.14.2
pkgrel=1
pkgdesc="HEIF image decoder/encoder library and tools (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/strukturag/libheif"
license=('spdx:LGPL-3.0' 'MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
             "${MINGW_PACKAGE_PREFIX}-cc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-aom"
         "${MINGW_PACKAGE_PREFIX}-dav1d"
         "${MINGW_PACKAGE_PREFIX}-libde265"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-rav1e"
         $( [[ ${CARCH} != x86_64 ]] || \
            echo "${MINGW_PACKAGE_PREFIX}-svt-av1" )
         "${MINGW_PACKAGE_PREFIX}-x265")
source=("https://github.com/strukturag/libheif/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('d016905e247d6952cd7ee4f9b90957350b6a6caa466bc76fdfe6eb302b6d088c')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  # Keep "-DX265_API_IMPORTS" flag due to https://github.com/strukturag/libheif/issues/357
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DX265_CFLAGS="-DX265_API_IMPORTS" \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/examples/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING-examples"
}
